package com.xiaoluo.web;


import com.xiaoluo.cd.entity.base.SysUser;
import com.xiaoluo.common.utils.AesUtils;
import com.xiaoluo.common.utils.QuickDeployShell;
import com.xiaoluo.service.KubernetesService;
import com.xiaoluo.service.UserService;
import com.xiaoluo.web.form.KubeServiceForm;
import com.xiaoluo.web.form.ResultForm;
import com.xiaoluo.web.form.SysUserForm;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Project : SpiderManager
 * Package : com.xiaoluo.web
 * Created by Willian.
 * Date  : 2015/10/21 20:29.
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private KubernetesService kubernetesService;

    @RequestMapping
    public  String index(ModelMap model){
        return "index";
    }
    @RequestMapping(value = "/login")
    public String login(Model model) {
        return "login";
    }

    @RequestMapping(value = "/dologin")
    public String doLogin(ModelMap model,@RequestParam String inputEmail,@RequestParam String inputPassword,HttpSession httpSession) {

        if(!"".equals(inputEmail) && !"".equals(inputPassword)){

            SysUserForm user = userService.find(inputEmail);

            if(user != null){
                String pwd = new String(AesUtils.decryptAES(user.getPassWord()));
                if(pwd.equals(inputPassword)){
                    setCurrentUser(user);
                    userService.save(user);
                    return "redirect:/";
                }
            }
        }
        model.addAttribute("msg","UserName or Password is Error.");
        return "login";
    }
    @RequestMapping(value = "/setting",method = RequestMethod.GET)
    public  String setting(ModelMap model){
        String path = System.getProperties().getProperty("xl.cd.workspace");
        String expire = System.getProperties().getProperty("xl.cd.expire");

        model.addAttribute("workspace",path);
        model.addAttribute("expire",expire);
        return "setting";
    }

    @RequestMapping(value = "/temp",method = RequestMethod.GET)
    public  String temp(ModelMap model){
        return "temp";
    }
    @RequestMapping(value = "/upload")
    @ResponseBody
    public ResultForm upload(@RequestParam(value = "file", required = false) MultipartFile file,
                             @RequestParam(value = "serverIp", required = false) String serverIp,
                             @RequestParam(value = "port", required = false) String port,
                             @RequestParam(value = "user", required = false) String user,
                             @RequestParam(value = "password", required = false) String password,//
                             @RequestParam(value = "serverDir", required = false) String serverDir,
                             @RequestParam(value = "cmd", required = false) String cmd,
                             HttpServletRequest request, ModelMap model){

        ResultForm result = new ResultForm(0,"");
        /**
         * 1、scp文件
         * 2、执行命令
         */
        String msg = QuickDeployShell.build(file, serverIp, port, user, password, serverDir, cmd);
        result.setMsg(msg);

        return result;
    }

    @RequestMapping(value = "/getPods",method = RequestMethod.GET)
    @ResponseBody
    public ResultForm getPod(ModelMap model){

        ResultForm result = new ResultForm(0,"sucessed");

        PodList pods = kubernetesService.getTestClient().pods().list();

        List<KubeServiceForm> list = new ArrayList<KubeServiceForm>();
        if(pods != null && pods.getItems().size() >0){
            for(Pod pod:pods.getItems()){
                KubeServiceForm form = new KubeServiceForm();
                form.setName(pod.getMetadata().getName());
                form.setPodIp(pod.getStatus().getPodIP());
                list.add(form);
            }
        }

        result.setData(list);

        return result;
    }

    @RequestMapping(value = "/shell",method = RequestMethod.GET)
    public String shell(ModelMap model,
                        @RequestParam(value = "type", required = false) String type,
                        @RequestParam(value = "ip", required = false) String ip){

        model.addAttribute("type",type);
        model.addAttribute("ip",ip);
        return "shell";
    }

}
